--- title: Title keywords: fastai sidebar: home_sidebar nb_path: "02_gsinstancesegmentation.ipynb" ---
This tutorial walk you through the different steps of training the fridge dataset. the IceVision Framework is an agnostic framework. As an illustration, we will train our model using both the fastai library, and pytorch-lightning libraries.
For more information about how the fridge dataset as well as its corresponding parser check out the pennfudan folder in icedata.
If on Colab run the following cell, else check the installation instructions
# check cuda version
import torch
cuda_version_major = int(torch.version.cuda.split('.')[0])
cuda_version_major
#install packages based on your cuda version
!wget https://raw.githubusercontent.com/airctic/icevision/master/install_colab.sh
!bash install_colab.sh {cuda_version_major}
#Restart kernel
import IPython
IPython.Application.instance().kernel.do_shutdown(True)
from icevision.all import *
TorchVision
model_type = models.torchvision.mask_rcnn
backbone = model_type.backbones.resnet34_fpn()
# Loading Data
data_dir = icedata.pennfudan.load_data()
train_ds, valid_ds = icedata.pennfudan.dataset(data_dir)
Note:
Transforms are applied lazily, meaning they are only applied when we grab (get) an item. This means that, if you have augmentation (random) transforms, each time you get the same item from the dataset you will get a slightly different version of it.
samples = [train_ds[0] for _ in range(3)]
show_samples(samples, ncols=3)
train_dl = model_type.train_dl(train_ds, batch_size=8, num_workers=4, shuffle=True)
valid_dl = model_type.valid_dl(valid_ds, batch_size=8, num_workers=4, shuffle=False)
model_type.show_batch(first(valid_dl), ncols=4)
model = model_type.model(backbone=backbone, num_classes=icedata.pennfudan.NUM_CLASSES)
metrics = [COCOMetric(metric_type=COCOMetricType.mask)]
IceVision is an agnostic framework meaning it can be plugged to other DL framework such as fastai2, and pytorch-lightning.
You could also plug to oth DL framework using your own custom code.
learn = model_type.fastai.learner(dls=[train_dl, valid_dl], model=model, metrics=metrics)
learn.lr_find()
learn.fine_tune(20, 1e-4, freeze_epochs=1)
class LightModel(model_type.lightning.ModelAdapter):
def configure_optimizers(self):
return SGD(self.parameters(), lr=1e-4)
light_model = LightModel(model, metrics=metrics)
trainer = pl.Trainer(max_epochs=20, gpus=1)
trainer.fit(light_model, train_dl, valid_dl)
model_type.show_results(model, valid_ds, detection_threshold=.5)
{% include note.html content='For a more detailed look at inference check out the inference tutorial' %}
infer_dl = model_type.infer_dl(valid_ds, batch_size=4, shuffle=False)
preds = model_type.predict_from_dl(model, infer_dl, keep_images=True)
show_preds(preds=preds[:4], ncols=3)
If you need any assistance, feel free to join our forum.